package com.rsa.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rsa.dto.LoginDTO;
import com.rsa.utils.PublicKeyMap;
import com.rsa.utils.RSAUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.SimpleDateFormat;
import java.util.HashMap;

@Controller
public class LoginController {
    @RequestMapping("/")
    public String loginHtml(Model model) {
        PublicKeyMap publicKeyMap = RSAUtil.getPublicKeyMap();
        String result = BeanToJson(publicKeyMap);
        model.addAttribute("rsa",result);
        return "login";
    }

    @RequestMapping("/login")
    public void login(LoginDTO loginDTO) {
        HashMap<String,Object> map = new HashMap<String, Object>();
        String loginName = RSAUtil.decryptStringByJs(loginDTO.getLoginName());
        String loginPsw = RSAUtil.decryptStringByJs(loginDTO.getPassword());
        System.out.println(loginName);
        System.out.println(loginPsw);

    }


    /**
     * 将baen对象转换为json 字符串
     * @param obj
     * @return
     */
    public static String BeanToJson(Object obj)
    {
        String ret = "";
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            ret = mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
